package com.gisuni.sddc.controller;

import java.util.Map;
import javax.servlet.http.HttpServletRequest;

import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.codehaus.jackson.map.ObjectMapper;
import org.json.JSONObject;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.GET;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

import com.gisuni.sddc.service.UserOrganizationService;
import com.gisuni.sddc.util.StringUtil;
import com.gisuni.sddc.model.Organization;
import com.gisuni.sddc.model.Response;
import com.gisuni.sddc.model.UserOrganization;

@IocBean(singleton = false)
@At("/client/service/userOrganizationController")
public class UserOrganizationController {

	private Log log = Logs.getLog(UserOrganizationController.class);

	@Inject
	private UserOrganizationService  userOrganizationService;

	
	/**
	 * 查询所有人员-组织数据
	 * 
	 * @return
	 */
	@At("/queryUserAll")
	@GET
	@POST
	@Ok("json")
	public Object queryBaseUserAll(@Param("userName") String userName,@Param("userId") String userId,@Param("page") int page, @Param("rows") int rows,HttpServletRequest request) {
		Response res = new Response();
		res.setSuccess(false);
		Object r = res;
		try {
			int from=(page-1)*rows;
			res = userOrganizationService.queryBaseUserAll(from,from+rows,userName,userId);
			JSONObject o = Response.toJSONObject(res);
			ObjectMapper mapper = new ObjectMapper();
			r = mapper.readValue(o.toString(), Map.class);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return r;
	}
	
	
	/**
	 * 修改数据
	 */
	@At("/updateUserOrganization")
	@GET
	@POST
	@Ok("json")
	public Object updataBaseUser(@Param("id") Long id,@Param("companyId") String companyId,@Param("businessId") String businessId,@Param("developId") String developId, @Param("existFlag")String existFlag ,HttpServletRequest request) {
		
		Response res = new Response();
		res.setSuccess(false);
		Object r = res;
		try {
			//只有管理员角色才能修改 TODO
//			String userInfo = (String) request.getSession().getAttribute("userInfo");
//			if(StringUtil.isNullOrEmpty(userInfo)||!userInfo.contains("管理员")){
//				return r;
//			}
			
			
			UserOrganization userOrganization=new UserOrganization();
			userOrganization.setUserId(id);
			userOrganization.setOrganizationCompanyId(companyId); 
			userOrganization.setOrganizationDevelopId(developId);
			userOrganization.setOrganizationBusinessId(businessId);
			res = userOrganizationService.updateUserOrganization(userOrganization,existFlag);
			JSONObject o = Response.toJSONObject(res);
			ObjectMapper mapper = new ObjectMapper();
			r = mapper.readValue(o.toString(), Map.class);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return r;
	}
	
	/**
	 * 查询组织数据
	 * @param parentId
	 * @return
	 */
	
	@At("/getOrganization")
	@GET
	@POST
	@Ok("json")
	public Object getOrganization(@Param("parentId") String parentId){
		Response res = new Response();
		res.setSuccess(false);
		Object r = res;
		try {
			Organization organization = new Organization();
			organization.setParentId(parentId);
			res = userOrganizationService.getOrganization(organization);
			JSONObject o = Response.toJSONObject(res);
			ObjectMapper mapper = new ObjectMapper();
			r = mapper.readValue(o.toString(), Map.class);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return r;
	}

	/**
	 * 
	 */
	
	/**
	 * 查询组织数据
	 * @param parentId
	 * @return
	 */
	
	@At("/getOrganizationData")
	@GET
	@POST
	@Ok("json")
	public Object getOrganizationData(@Param("organizationName") String name,@Param("managerUserId") String managerUserId){
		Response res = new Response();
		res.setSuccess(false);
		Object r = res;
		try {
			Organization organization = new Organization();
			organization.setOrganizationName(name);;
			organization.setOrganizationMenagerId(managerUserId);
			res = userOrganizationService.getOrganizationData(organization);
			JSONObject o = Response.toJSONObject(res);
			ObjectMapper mapper = new ObjectMapper();
			r = mapper.readValue(o.toString(), Map.class);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return r;
	}
	
	/**
	 * 删除数据
	 * 
	 * @param UserId
	 * @return
	*/
	@At("/deleteOrganization")
	@GET
	@POST
	@Ok("json")
	public Object deleteOrganization(@Param("organizationId") String Id, HttpServletRequest request) {
		Response res = new Response();
		res.setSuccess(false);
		Object r = res;
		try {
			Organization organization=new Organization();
			organization.setId(Id);
			res = userOrganizationService.deleteOrganization(organization);
			JSONObject o = Response.toJSONObject(res);
			ObjectMapper mapper = new ObjectMapper();
			r = mapper.readValue(o.toString(), Map.class);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return r;
	}
	

	/**
	 * 添加数据
	 * 
	 * @param UserName
	 * @param UserNote
	 * @return
	*/ 
	@At("/addOrganization")
	@GET
	@POST
	@Ok("json")
	public Object addOrganization(@Param("organizationMenagerId") String organizationMenagerId,@Param("organizationName") String organizationName, @Param("organizationType") String organizationType, @Param("parentId") String parentId ,HttpServletRequest request) {
		Response res = new Response();
		res.setSuccess(false);
		Object r = res;
		try {
			Organization organization = new Organization();
			organization.setOrganizationMenagerId(organizationMenagerId);
			organization.setOrganizationName(organizationName);
			organization.setOrganizationType(organizationType);
			organization.setParentId(parentId);
 
			res = userOrganizationService.addOrganization(organization);
			JSONObject o = Response.toJSONObject(res);
			ObjectMapper mapper = new ObjectMapper();
			r = mapper.readValue(o.toString(), Map.class);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return r;
	}
 
	/**
	 * 更新数据
	 * 
	 * @param UserName
	 * @param UserNote
	 * @return
	*/ 
	@At("/updateOrganization")
	@GET
	@POST
	@Ok("json")
	public Object updateOrganization(@Param("organizationId") String id,@Param("organizationMenagerId") String organizationMenagerId,@Param("organizationName") String organizationName,@Param("type") int Type,@Param("organizationType") String organizationType, @Param("parentId") String parentId ,HttpServletRequest request) {
		Response res = new Response();
		res.setSuccess(false);
		Object r = res;
		try {
			Organization organization = new Organization();
			organization.setId(id);
			organization.setOrganizationMenagerId(organizationMenagerId);
			organization.setOrganizationName(organizationName);
			organization.setOrganizationType(organizationType);
			organization.setParentId(parentId);
 
			res = userOrganizationService.updateOrganization(organization);
			JSONObject o = Response.toJSONObject(res);
			ObjectMapper mapper = new ObjectMapper();
			r = mapper.readValue(o.toString(), Map.class);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return r;
	}
	

	 
 
 
}
